<?php

namespace App\Console;

use App\City;
use Backup;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use PDO;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->call(function (){
//             $now = Carbon::now();
//             $six_hours = Carbon::today()->addHours($_ENV['BACKUP_HOUR']);
//             $six_hours_minute = Carbon::today()->addHours($_ENV['BACKUP_HOUR'])->addMinutes(1);
//             if ($now->between($six_hours,$six_hours_minute)){
                 $filename='Auto-'.date("Ymd-His").".sql"; //生成sql文件名
                 header("Content-disposition:filename=".$filename);
                 header("Content-type:application/octetstream");
                 header("Pragma:no-cache");
                 header("Expires:0");
                 $filepath = $_ENV['BACKUP_FILE'].$filename; //文件保存路径
                 exec($_ENV['MYSQL_BIN']."mysqldump.exe  -uroot --default-character-set=utf8 school-line -n > ".$filepath); //所有表结构和数据
                 \Log::info('yes ------------> '.date('H:i:s'));
//             }else{
//                 \Log::info('now------------->'.$now);
//                 \Log::info('hours------------->'.$six_hours);
//                 \Log::info('minutes------------->'.$six_hours_minute);
//                 \Log::info('no ------------> '.date('H:i:s'));
//             }
         })->everyMinute();

    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}
